package com.ssm.changeunit.console.parser;

import com.ssm.changeunit.command.*;
import com.ssm.changeunit.persistance.ChangeUnitDAO;
import org.apache.commons.cli.*;

/**
 * TagConsoleParser
 * 
 * type - add, delete, list
 * changeunit - ChangeUnit의 Id를 적는 곳입니다.
 * name - Tag의 이름을 나타냅니다.
 * 
 * ex) ChangeUnitConsole tag -name "test" -changeunit "changeunitid"
 * 
 * @author 시리우스
 */
public class TagConsoleParser extends ConsoleParser {
    @Override
    public CommandWorkerOptions parse(java.lang.String[] userCommand) throws ConsoleParseException {
        try {
            // 파서 생성
            CommandLineParser parser = new GnuParser();

            Options options = new Options();

            Option type = OptionBuilder.withLongOpt("type").hasArg().isRequired().create();
            Option changeunit = OptionBuilder.withLongOpt("changeunit").hasArg().create();
            Option name = OptionBuilder.withLongOpt("name").hasArg().create();

            options.addOption(type);
            options.addOption(changeunit);
            options.addOption(name);

            CommandLine commandLine = parser.parse(options, userCommand);

            CommandWorkerOptions commandWorkerOptions = new CommandWorkerOptions();
            
            // changeunit 프로퍼티가 있는지 검사
            if(commandLine.hasOption("type")) {                
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("type");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String item = commandLine.getOptionValue("type");
                
                commandWorkerOption.addValue(item);
            }
            else {
                // 에러 발생
                throw new ConsoleParseException("type 프로퍼티가 존재하지 않습니다.");
            }
            
            // changeunit 프로퍼티가 있는지 검사
            if(commandLine.hasOption("changeunit")) {                
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("changeunit");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String item = commandLine.getOptionValue("changeunit");
                
                commandWorkerOption.addValue(item);
            }
            
            // name 프로퍼티가 있는지 검사
            if(commandLine.hasOption("name")) {                
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("name");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String item = commandLine.getOptionValue("name");
                
                commandWorkerOption.addValue(item);
            }
            
            return commandWorkerOptions;
        }
        catch(ParseException ex) {
            throw new ConsoleParseException(ex);
        }
    }
}

